package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.company.SearchCompanyBo;
import com.zhuss.bean.vo.core.company.DetailCompanyVo;
import com.zhuss.bean.vo.core.company.ListCompanyVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.common.SelectVo;
import com.zhuss.core.db.dao.ext.CompanyDao;
import com.zhuss.core.db.entity.Company;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.OceanException;


/**
 * 查询公司 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("companyRead")
public class CompanyRead {
	
	@Autowired
	private CompanyDao companyDao;
	
	
	public Map<String, Company> listMap() {
		List<Company> list = companyDao.listAll();
		Map<String, Company> map = new HashMap<>();
		for (Company c : list) {
			map.put(c.getName(), c);
		}
		return map;
	}
	
	
	/**
	 * 根据公司类型查询公司列表
	 * @param 
	 * @return
	 */
	public List<SelectVo> listSelectByType(Integer type) {
		List<SelectVo> result = new ArrayList<SelectVo>();
		List<Company> companyList = listByType(type);
		for (Company c : companyList) {
			SelectVo sv = new SelectVo(c.getCompanyId().intValue(), c.getName(), "");
			result.add(sv);
		}
		return result;
	}
	
	
	/**
	 * 根据公司类型查询公司列表
	 * @param 
	 * @return
	 */
	public List<Company> listByType(Integer type) {
		List<Company> result = companyDao.listByType(type);
		return result;
	}
	
	/**
	 * 根据名字查询公司
	 * @param 
	 * @return
	 */
	public List<Company> list(String name) {
		List<Company> result = companyDao.listByName(name);
		return result;
	}
	
	public List<Company> listNotId(String name, Long companyId) {
		List<Company> result = new ArrayList<Company>();
		List<Company> list = list(name);
		for (Company c : list) {
			if (!c.getCompanyId().equals(companyId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	/**
	 * 公司
	 * @return
	 */
	public List<Company> listCompany() {
		List<Company> result = companyDao.listAll();
		return result;
	}
	
	
	/**
	 * 公司
	 * @return
	 */
	public Object listCompanySelect() {
		List<Company> companyList = companyDao.listAll();
		List<ListVo> result = new ArrayList<>();
		transferList(companyList, result);
		return result;
	}
	
	/**
	 * 公司 
	 * @return
	 */
	public Object listCompanyId() {
		List<Company> companyList = companyDao.listAll();
		List<Long> result = new ArrayList<>();
		transferListId(companyList, result);
		return result;
	}
	
	private void transferListId(List<Company> companyList, List<Long> result) {
		for (Company item : companyList) {
			result.add(item.getCompanyId());
		}
	}

	private void transferList(List<Company> companyList, List<ListVo> result) {
		for (Company item : companyList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getCompanyId());
			vo.setLabel(item.getName());
			result.add(vo);
		}
		
	}

	/**
	 * 公司列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchCompanyBo bo) {
		PageVo<ListCompanyVo> result = new PageVo<ListCompanyVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(companyDao.list(bo));
        result.setTotal(companyDao.countList(bo));
		return result;
	}
	
	/**
	 * 公司详情
	 * @param companyId
	 * @return
	 * @throws OceanException
	 */
	public DetailCompanyVo detail(Long companyId) throws OceanException {
		DetailCompanyVo result = companyDao.detail(companyId);
		if (result == null) {
			throw new OceanException(ExceptionCode.数据不存在);
		}
		return result;
	}
	
	

}
